import * as actionTypes from "./actionTypes";
import { getSearchList, getNovelById } from '../../../apis/novel';
import axios from 'axios';
import {
    time2Str,
    url2Real,
    wordCount2Str
} from '../../../static/js/index';
export const changeSearchList = (result) => ({
    type: actionTypes.ChangeSearchList,
    result,
})
export const delHistory = () => ({
    type: actionTypes.DelHistory,
})
export const changenovelDetailsData = (result) => ({
    type: actionTypes.ChangenovelDetailsData,
    result,
})
export const collectNoverById = (id) => ({
    type: actionTypes.CollectNoverById,
    id,
})
export const collectNoverByIdList = (result) => ({
    type: actionTypes.CollectNoverByIdList,
    result,
})
export const collectNoverByIdDe = (id) => ({
    type: actionTypes.CollectNoverByIdDe,
    id,
})
//页面初始化
export const _getSearchList = (val) => {
    return (dispatch) => {
        // console.log(val,2222)
        getSearchList(val).then(res => {
            console.log(1212, '*&&%%', res)
            // eslint-disable-next-line
            res.books.map((item) => { item.cover = url2Real(item.cover) });
            let result = {
                serchKey: val,
                myColletList: res
            }
            dispatch(changeSearchList(result))
        })
    }
}
export const _getNovelById = (val) => {
    return (dispatch) => {
        getNovelById(val).then(res => {
            console.log('_getNovelById', '*&&%%', res)
            res.cover = url2Real(res.cover)
            res.wordCount = wordCount2Str(res.wordCount);
            res.updated = time2Str(res.updated);
            dispatch(changenovelDetailsData(res))
        })
    }
}
export const _getcollectNoverByIdList = (val) => {
    return (dispatch) => {
        // let result = [...val];
        let arr = val.map(i => {
            return getNovelById(i)
        })
        axios.all(arr).then(axios.spread((...result) => {
            result.map(res => {
                res.cover = url2Real(res.cover)
                res.wordCount = wordCount2Str(res.wordCount);
                res.updated = time2Str(res.updated);
                return res
            })
            console.log(1213, result)
            dispatch(collectNoverByIdList(result))
        }));
    }
}
